package com.tmt.im.manager.service;

import com.tmt.data.jpa.service.beans.UserDetailsImpl;
import com.tmt.im.jpa.entity.IMUser;
import com.tmt.im.jpa.repository.IMUserRepo;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description TODO
 * @Author wuyi
 * @Date 2024/7/18 11:04
 * @Version 1.0
 **/
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private IMUserRepo userRepo;

    @Override
    @Transactional(readOnly = true)
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        IMUser user = userRepo.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("登录账号不存在");
        }
        return new UserDetailsImpl(user.getId(), username, username, user.getNickname(), user.getPassword(), user.getMobile(),
                user.getDeviceType(), user.getDeviceIds(), user.getAuthorities());
    }

}
